<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item
            label="附件名称"
            prop="name"
            :rules="[{ required: true, message: '请选择附件名称关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.name" clearable placeholder="请选择附件名称关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="后缀名"
            prop="suffix"
            :rules="[{ required: true, message: '请选择后缀名关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.suffix" clearable placeholder="请选择后缀名关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="存储类型"
            prop="type"
            :rules="[{ required: true, message: '请选择存储类型', trigger: 'change' }]"
          >
            <el-select v-model="form.type" clearable placeholder="请选择存储类型">
              <el-option
                v-for="obj in types"
                :key="obj.id +'-type'"
                :label="obj.name"
                :value="obj.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="附件内容"
            prop="content"
            :rules="[{ required: true, message: '请选择附件内容关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.content" clearable placeholder="请选择附件内容关联字段">
              <el-option
                v-for="obj in params"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>

<script>
import { updateObject } from '@/api/portal/manager/entityTemp';
export default {
  name: 'ShowEntityV1',
  props: {
    column: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      params: [],
      types: [
        { id: 1, code: 'blob', name: '文件流' },
        { id: 2, code: 'base64', name: 'Base64' },
        { id: 3, code: 'url', name: '下载地址' }
      ],
      form: {
        name: '', // 附件名称
        suffix: '', // 附件类型
        type: '', // 获取方式，1： 文件流，2：base64,3：下载地址
        content: '' // 附件内容管理字段
      }
    };
  },
  watch: {
    column: {
      handler(newName, oldName) {
        if (undefined !== newName.jxx && newName.jxx !== null) {
          this.params = JSON.parse(newName.jxx);
        }
        if (undefined !== newName.pzx && newName.pzx !== null) {
          this.form = JSON.parse(newName.pzx);
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    save(formName) {
      this.loading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.column.pzx = JSON.stringify(this.form);
          updateObject(this.column).then(res => {
            this.$message.success('更新成功！');
            this.close();
          }).catch(() => {
            this.loading = false;
            this.$message.error('更新失败！');
          });
        } else {
          this.loading = false;
          console.log('error submit!!');
          return false;
        }
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    }
  }
};
</script>

<style scoped>
.setting-body {
  width: 100%;
}
.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}
.custom-button {
  text-align: right;
}
</style>

